package com.service.business.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.order.controller.rep.OrderProductOtherDetailsRep;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.model.OrderProductOther;
import com.service.business.order.dto.ProductOtherCar;
import com.service.business.order.service.OrderProductOtherDetailsService;
import com.service.dao.mapper.OrderProductOtherDetailsMapper;
import com.service.dao.model.OrderProductOtherDetails;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("orderDetailsService")
public class OrderProductOtherDetailsServiceImpl extends ServiceImpl<OrderProductOtherDetailsMapper, OrderProductOtherDetails> implements OrderProductOtherDetailsService {


    @Override
    public void createProductOrderDetail(List<ProductOtherCar> productOtherCar, OrderProductOther orderProductOther) {

        List<OrderProductOtherDetails> detailsList = productOtherCar.stream().map(item -> {
            OrderProductOtherDetails orderProductOtherDetails = ModelMapperUtil.getStrictModelMapper().map(item, OrderProductOtherDetails.class);
            orderProductOtherDetails.setProductId(item.getId());

            CoinsEnum coinsEnum = CoinsEnum.valueOf(item.getProductPriceCoin());

            orderProductOtherDetails.setProductCoinsId(coinsEnum.getCode());
            orderProductOtherDetails.setProductCoinsName(coinsEnum.getName());

            coinsEnum = CoinsEnum.valueOf(item.getGiftCoins());
            orderProductOtherDetails.setGiftCoinsId(coinsEnum.getCode());
            orderProductOtherDetails.setGiftCoinsName(coinsEnum.getName());

            coinsEnum = CoinsEnum.valueOf(item.getFreightCoin());
            orderProductOtherDetails.setFreightCoinsId(coinsEnum.getCode());
            orderProductOtherDetails.setFreightCoinsName(coinsEnum.getName());

            orderProductOtherDetails.setProductPic(item.getPic());
            orderProductOtherDetails.setCreateTime(new Date());
            orderProductOtherDetails.setOrderId(orderProductOther.getId());
            return orderProductOtherDetails;
        }).collect(Collectors.toList());

        saveBatch(detailsList);

    }

    @Override
    public Integer getProductOtherBuyNum(Integer memberId, Integer productId, List<Integer> buyStatus) {
        return baseMapper.getBuyNumByMemberIdAndProductId(memberId, productId, buyStatus);
    }

    @Override
    public List<OrderProductOtherDetailsRep> listByOrderId(Integer orderId) {
        return list(new QueryWrapper<OrderProductOtherDetails>().lambda().eq(OrderProductOtherDetails::getOrderId, orderId))
                .stream().map(item -> {
                    OrderProductOtherDetailsRep resultItem = ModelMapperUtil.map(item, OrderProductOtherDetailsRep.class);
                    resultItem.setProductCoin(item.getProductCoinsName());
                    resultItem.setGiftCoins(item.getGiftCoinsName());
                    resultItem.setFreightCoins(item.getFreightCoinsName());
                    return resultItem;
                }).collect(Collectors.toList());
    }
}
